import {handler, view} from "adajs";
import LodgeService from "./state.js";
import TabWin from "./../../../controls/tab/win";
import Bar from "../../../controls/bar";
import Page from "./../../tabpage";
import Messagebox from "../../../controls/messagebox";
import Form from "../../../controls/form";
import Text from "../../../controls/form/text";
import Input from "./../../../controls/form/input";
import Loading from "../../../controls/loading";
import User from "./../../../lib/user";

@view({
    className: "lodge",
    template: "./template.html",
    style: "./style.scss",
    dataset: {
        service: LodgeService
    }
})
class Lodge extends Page {
    onready() {
        this.commit("getZBxx");
    }

    tags() {
        return {
            tab: TabWin,
            bar: Bar
        }
    }

    @handler("bar-addactivity")
    add() {
        this.addChild(Messagebox, {
            name: "box",
            parameter: {
                title: "新增交流",
                content: [
                    {
                        type: Form, option: {
                            fields: [
                                {name: "theme", type: Input, label: "主题", required: true},
                                {name: "content", type: Text, label: "内容", required: true}
                            ]
                        }
                    }
                ],
                btns: [
                    {name: "关闭", action: "close"},
                    {name: "新增", action: "comment"}
                ]
            }
        })
    }

    @handler("bar-addthorey")
    addT() {
        this.addChild(Messagebox, {
            name: "boxt",
            parameter: {
                title: "新增主题",
                content: [
                    {
                        type: Form, option: {
                            fields: [
                                {name: "theme", type: Input, label: "主题", required: true},
                                {name: "content", type: Text, label: "内容", required: true}
                            ]
                        }
                    }
                ],
                btns: [
                    {name: "关闭", action: "close"},
                    {name: "新增", action: "commentt"}
                ]
            }
        })
    }

    @handler("comment")
    comment(e) {
        let form = e.target.getChildAt(0);
        form.check().then(a => {
            if (a) {
                form.getValue().then(values => {
                    this.addChild(Loading).then(loading => {
                        this.context.request.post("/api/lgb/app/lgb/branchexchange/insert", Object.assign({
                            type: 'exchange'
                        }, values)).then((a) => {
                            if (a.code === '1') {
                                loading.showSuccess("添加成功");
                                loading.close().then(() => {
                                    this.getChildByName("box").close();
                                    this.commit("refreshList");
                                });
                            } else {
                                loading.showError(a.msg);
                                loading.close();
                            }
                        }, () => {
                            loading.showError('服务异常');
                            loading.close();
                        })
                    });
                });
            }
        });
    }

    @handler("commentt")
    commentt(e) {
        let form = e.target.getChildAt(0);
        form.check().then(a => {
            if (a) {
                form.getValue().then(values => {
                    this.addChild(Loading).then(loading => {
                        this.context.request.post("/api/lgb/app/lgb/branchexchange/insert", Object.assign({
                            type: 'theory'
                        }, values)).then((a) => {
                            if (a.code === '1') {
                                loading.showSuccess("添加成功");
                                loading.close().then(() => {
                                    this.getChildByName("boxt").close();
                                    this.commit("refreshListx");
                                });
                            } else {
                                loading.showError(a.msg);
                                loading.close();
                            }
                        }, () => {
                            loading.showError('服务异常');
                            loading.close();
                        })
                    });
                });
            }
        });
    }

    @handler('showList')
    showList() {
        this.commit('showList');
    }
}

export default Lodge;